package com.company.cloud.goodsrestservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.company.cloud.goodsrestservice.core.AbstractService;
import com.company.cloud.goodsrestservice.dao.*;
import com.company.cloud.goodsrestservice.model.*;
import com.company.cloud.goodsrestservice.service.SkuService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * Created by reabout on 2018/03/05.
 */
@Service
@Transactional
public class SkuServiceImpl extends AbstractService<Sku> implements SkuService {
	@Resource
    private SkuMapper skuMapper;

	@Resource
	private PictureMapper PictureMapper;
	@Value("${picture.base.url}")
	private String pictureUrl;

    //	@Value("${picture.root_path}")
//	private String picPath;
	
	
	private  List<Sku> getSkuList(String where){
		return skuMapper.getList(where);
	}

	private  List<Sku> getSkuListWithPicure(String where){
		List<Sku> list = this.getSkuList(where);
        return addPictureUrl(list);
	}

	public List<Sku> addPictureUrl(List<Sku> skuList) {
        if (null != skuList & !skuList.isEmpty()) {
            for (Sku sku : skuList) {
                Integer goodsId = (sku.getGoodsId() == null) ? 0 : sku.getGoodsId();
                String url = "";
                if (goodsId != 0) {
                    Picture picutre = PictureMapper.getPicture(goodsId, 0);
                    String picturePath = (picutre == null) ? "" : picutre.getPicturePath();
                    if (!picturePath.isEmpty()) {
                        url = pictureUrl + "/" + picutre.getPicturePath();
                    }
                }
                sku.setPictureUrl(url);
            }
        }
        return skuList;
    }

	public List<Sku> getSkuListByTime(Long startTime,Long endTime){
		String where = "sc.update_time between '"+startTime.toString()+"' and '"+endTime.toString()+"'";
		return this.getSkuListWithPicure(where);
	}
	
	public List<Sku> getSkuByMaterialNumber(String materialNumber){
		String where = "sc.sku_material_number ='"+ materialNumber +"'";
		return this.getSkuListWithPicure(where);
	}
	//更新返回影响行数
	public Integer updateStock(String materialNumber,  Integer stock, Integer locationId ) {
		Integer reslut = skuMapper.updateStock(materialNumber,stock,locationId );
		//日志记录
		return reslut;
	}

    public List<Sku> getSkuByOrderId(Integer orderId) {
	    String where = "rso.order_id=" + orderId;
		List<Sku> list = skuMapper.getSkuByOrderId(where);
        return addPictureUrl(list);
    }

	public List<Sku> getSkuByGoodsNumber(String goodsNumber) {
		String where = "g.goods_number='" + goodsNumber + "'";
		return getSkuListWithPicure(where);
	}

	public List<Sku> getSkuBySkuName(String skuName) {
		String where = "sc.sku_name='" + skuName + "'";
		return getSkuListWithPicure(where);
	}

	public List<Sku> getSkuByCategory(String category) {
		String where = "g.category_name='" + category + "'";
		return getSkuListWithPicure(where);
	}

	public List<Sku> getSkuBySearch(Object value) {
	    String where = "sc.sku_material_number ='" + value.toString() + "'";
	    where += " or g.goods_number='" + value.toString() + "'";
	    where += " or sc.sku_name='" + value.toString() + "'";
	    where += " or g.category_name='" + value.toString() + "'";
	    return getSkuListWithPicure(where);
    }

    public List<Sku> getSkuByJson(JSONObject sku) {
        Map<String, String> columnMap= new HashMap<>();
        columnMap.put("goodsNumber", "g.goods_number");
        columnMap.put("materialNumber", "sc.sku_material_number");
        columnMap.put("skuName", "sc.sku_name");
        columnMap.put("categoryName", "g.category_name");
        columnMap.put("orderId", "rso.order_id");
        Sku sk = new Sku();
        String where = "1=1";
        Set<String> keys = sku.keySet();
        for (String key:keys) {
            try  {
                Field field = sk.getClass().getDeclaredField(key);
                if (field.getType() == Integer.class) {
                    where += " and " + columnMap.get(key) + "=" + sku.getString(key);
                }else {
                    where += " and " + columnMap.get(key) + "='" + sku.getString(key) + "'";
                }
            } catch (Exception e) {
                continue;
            }
        }
        List<Sku> list;
        if (keys.contains("orderId")){
            list = skuMapper.getSkuByOrderId(where);
        } else {
            list = skuMapper.getList(where);
        }
        return addPictureUrl(list);
    }

	public List<Sku> getSkuAll() {
		String where = "1=1";
		return getSkuList(where);
	}

}
